package com.lanou3g.dllo.baidumusic.main.playerdetail.lyrics;

import android.view.View;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseFragment;
import com.lanou3g.dllo.baidumusic.event.ProgressEvent;
import com.lanou3g.dllo.baidumusic.main.playerdetail.PlayDetailBean;
import com.lanou3g.dllo.baidumusic.main.playerdetail.lyrics.LyricsView;
import com.lanou3g.dllo.baidumusic.main.playmusic.SongPlayBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by dllo on 16/7/1.
 */
public class LrcFragment extends BaseFragment {

    private LyricsView lyricView;

    @Override
    public int setLayout() {
        EventBus.getDefault().register(this);
        return R.layout.fragment_lrc;
    }

    @Override
    public void initView(View view) {
        lyricView = (LyricsView) view.findViewById(R.id.playmusic_lrcview);

    }

    @Override
    public void initData() {
        //刚刚跳转到播放界面传来的歌词
        if (getActivity().getIntent().getParcelableExtra("playDetailBean") != null) {
            PlayDetailBean playDetailBean = getActivity().getIntent().getParcelableExtra("playDetailBean");
            String lrcLink = playDetailBean.getLrcLink();
            loadLrc(lrcLink);
        }
    }

    public void loadLrc(String lrc) {
        lyricView.loadLrc(lrc);
    }

    public void onPublish(int progress) {
        if (lyricView.hasLrc()) {
            lyricView.updateTime(progress);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void SeekBarControl(SongPlayBean bean) {
        loadLrc(bean.getSonginfo().getLrclink());
    }

    //EventBus接收消息设置时间
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void radioPlayTime(ProgressEvent progressEvent) {
        onPublish(progressEvent.getProgress());
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
